package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoanapplicationDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.dao.RequestDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Loanapplications;
import com.cashmate.hibernate.pojo.Loanverification;
import com.cashmate.hibernate.pojo.Request;
import com.cashmate.hibernate.pojo.Usertypes;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class Request_UpdateRequest extends ActionSupport {

    private RequestDAO requestDAO = (RequestDAO) ServletActionContext.getServletContext().getAttribute("requestDAO");
    private LoanverificationDAO loanverDAO = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private String selected;
    private String loc;

    @Actions({
        @Action(value = "/updaterequest", results = {
            @Result(location = "${loc}", name = "success", type = "redirect"),
            @Result(location = "${loc}", name = "input")
        })
    })
    @Override
    public String execute() throws Exception {
        Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");
        Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");
        
        List<Request> reqs = new ArrayList<Request>();
        String[] sel = selected.split(",");
        Integer[] sel_int = new Integer[sel.length];
        int count = 0;
        for (String s : sel) {
            sel_int[count] = Integer.parseInt(s);
            count++;
        }
        
        DetachedCriteria criteria = DetachedCriteria.forClass(Request.class);
        criteria.add(Restrictions.in("requestid", sel_int));
        criteria.setProjection(null);
        criteria.setResultTransformer(Criteria.ROOT_ENTITY);
        reqs = requestDAO.findByCriteria(criteria, 0, sel_int.length);
        
        for(Request r : reqs){            
            List<Loanverification> list;
            if (!b.getBranchcode().equalsIgnoreCase("HO")) {
                r.setStatus("Verified");
                requestDAO.update(r);
                    DetachedCriteria criteria2 = DetachedCriteria.forClass(Loanverification.class);
                    criteria2.createCriteria("request", "req").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                    criteria2.add(Restrictions.eq("req.requestid", r.getRequestid()));
                    int rec = loanverDAO.countByCriteria(criteria2);
                    criteria2.setProjection(null);
                    criteria2.setResultTransformer(Criteria.ROOT_ENTITY);
                list = loanverDAO.findByCriteria(criteria2, 0, rec);
                for (Loanverification l : list) {
//                    l.setStatus("BranManApproved");
                    l.setStatus("Verified");
                    loanverDAO.update(l);
                }
                loc = "/loanrequest.jsp?cat=branchman";
            } 
//            else {
//                r.setStatus("Verified");
//                requestDAO.update(r);
//                    DetachedCriteria criteria2 = DetachedCriteria.forClass(Loanverification.class);
//                    criteria2.createCriteria("request", "req").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
//                    criteria2.add(Restrictions.eq("req.requestid", r.getRequestid()));
//                    int rec = loanverDAO.countByCriteria(criteria2);
//                    criteria2.setProjection(null);
//                    criteria2.setResultTransformer(Criteria.ROOT_ENTITY);
//                list = loanverDAO.findByCriteria(criteria2, 0, rec);
//                for (Loanverification l : list) {
//                    l.setStatus("Verified");
//                    loanverDAO.update(l);
//                }
//                loc = "/loanrequest.jsp?cat=headoffice";
//            }
        }

        return SUCCESS;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public String getLoc() {
        return loc;
    }
}
